package com.lsz.word.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 获取词语表中全部的单词
 * @author lsz
 *
 */
public class Words {
	
	/**
	 * 获取文本文件中全部的单词
	 * @param filepath
	 * @return
	 * @throws IOException
	 */
	public static List<String> words(String filepath) throws IOException{
	
		String content = readString(filepath);

		return regWords(content);
	}
	/**
	 * 根据正则提取出字符串中的内容
	 * @param content
	 * @return
	 */
	public static List<String> regWords(String content){
		List<String> list = new ArrayList<String>();
		
		final Pattern pa = Pattern.compile("[A-Za-z]+");
		final Matcher ma = pa.matcher(content);
		
		while (ma.find()) {
			list.add(ma.group());
		}
		return list;
	}
	
	/**
	 * 读取指定文件的全部内容
	 * @param filepath
	 * @return
	 * @throws IOException
	 */
	public static String readString(String filepath) throws IOException{
		String content = "";
		StringBuffer sbf = new StringBuffer();
		BufferedReader br = new BufferedReader(new  InputStreamReader(new FileInputStream(new File(filepath))));
		String tmp = "";
		while((tmp = br.readLine())!=null){
			sbf.append(tmp);
			
		}
		content = sbf.toString();
		return content;
	}
	
	/**
	 * 将数组中的全部的单词转换为小写的
	 * @param list
	 * @return
	 */
	public static List<String> allToLow(List<String> list){
		List<String> lowList = new ArrayList<String>();
		for(String str:list){
			lowList.add(str.toLowerCase());
		}
		return lowList;
	}
	public static void main(String args[]) throws IOException{
			
		List<String> list = words("big.txt");
		System.out.println(list.size());
		allToLow(list);
	}
}
